package com.tjhk.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tjhk.test.entity.User;
import com.tjhk.test.model.Result;
import com.tjhk.test.model.ResultGenerator;
import com.tjhk.test.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "SQLite测试")
@RestController
@RequestMapping("api.user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", required = false),
            @ApiImplicitParam(name = "name", value = "用户姓名", required = true),
            @ApiImplicitParam(name = "height", value = "用户身高", required = false),
            @ApiImplicitParam(name = "intro", value = "自我介绍", required = false)
    })
    @ApiOperation("增")
    @PostMapping("/user")
    public Result add(User user) {
        if (userService.save(user) == 0) {
            return ResultGenerator.genFailResult("新增用户失败，请稍后重试!");
        }
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParam(name = "id", value = "要查询的用户id", required = true)
    @ApiOperation("删")
    @DeleteMapping("/user")
    public Result deleteById(Integer id) {
        if (userService.removeById(id) == 0) {
            return ResultGenerator.genFailResult("删除用户失败，请稍后重试!");
        }
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParam(name = "id", value = "要查询的用户id", required = true)
    @ApiOperation("查")
    @GetMapping("/user")
    public Result<User> queryById(Integer id) {
        User user = userService.getById(id);
        if (user == null) {
            return ResultGenerator.genFailResult("没有此用户！");
        }
        return ResultGenerator.genSuccessResult(user);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", required = true),
            @ApiImplicitParam(name = "name", value = "用户姓名", required = false),
            @ApiImplicitParam(name = "height", value = "用户身高", required = false),
            @ApiImplicitParam(name = "intro", value = "自我介绍", required = false)
    })
    @ApiOperation("改")
    @PutMapping("/user")
    public Result updateById(User user) {
        if (userService.updateById(user) == 0) {
            return ResultGenerator.genFailResult("更新用户失败，请稍后重试!");
        }
        return ResultGenerator.genSuccessResult();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", required = true),
            @ApiImplicitParam(name = "size", value = "每页数据", required = false)
    })
    @ApiOperation("分页")
    @GetMapping("/user/list")
    public Result<IPage<User>> page(Integer current
            , @RequestParam(value = "size", required = false, defaultValue = "12") Integer size) {
        IPage<User> page = userService.page(current, size);
        if (page == null) {
            return ResultGenerator.genFailResult("未查询到用户！");
        }
        return ResultGenerator.genSuccessResult(page);
    }

}
